SlideShare a Scribd company logo
Class No.09  Data Structures http://ecomputernotes.com
Memory Organization Code Static data Stack Heap Process 1 (browser) Process 3 (word) Process 4 (excel) Windows OS Process 2 (dev-c++) http://ecomputernotes.com
Stack Layout during a call Here is stack layout when function F calls function G: Parameters(F) Local variables(F) Return address(F) Parameters(G) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) Parameters(G) Local variables(G) Return address(G) During execution of G After call At point of call sp sp sp http://ecomputernotes.com
Queues A stack is LIFO (Last-In First Out) structure.  In contrast, a  queue  is a FIFO (First-In First-Out ) structure. A queue is a linear structure for which items can be only inserted at one end and removed at another end. http://ecomputernotes.com
Queue Operations Enqueue(X) – place X at the  rear  of the queue. Dequeue() -- remove the  front  element and return it. Front() -- return front element without removing it. IsEmpty() -- return TRUE if queue is empty, FALSE otherwise http://ecomputernotes.com
Implementing Queue Using linked List:  Recall Insert works in constant time for either end of a linked list. Remove works in constant time only. Seems best that head of the linked list be the front of the queue so that all removes will be from the front. Inserts will be at the end of the list. http://ecomputernotes.com
Implementing Queue Using linked List: front 2 5 7 1 1 7 5 2 front rear rear http://ecomputernotes.com
Implementing Queue Using linked List: front 2 5 7 1 1 7 5 2 front rear rear front 2 5 7 1 7 5 2 front rear rear dequeue() http://ecomputernotes.com
Implementing Queue Using linked List: front 2 5 7 1 1 7 5 2 front rear rear front 2 5 7 9 7 5 2 front rear rear enqueue(9) 9 http://ecomputernotes.com
Implementing Queue int dequeue() { int x = front->get(); Node* p = front; front = front->getNext(); delete p; return x; } void enqueue(int x) { Node* newNode = new Node(); newNode->set(x); newNode->setNext(NULL); rear->setNext(newNode); rear = newNode; } http://ecomputernotes.com
Implementing Queue int front() { return front->get(); } int isEmpty() { return ( front == NULL ); } http://ecomputernotes.com
Queue using Array If we use an array to hold queue elements, both insertions and removal at the front (start) of the array are expensive. This is because we may have to shift up to “n” elements. For the stack, we needed only one end; for queue we need both. To get around this, we will not shift upon removal of an element. http://ecomputernotes.com
Queue using Array front 2 5 7 1 rear 6 5 7 0 0 1 3 2 4 front 1 7 5 2 3 rear http://ecomputernotes.com
Queue using Array front 2 5 7 1 rear 6 5 7 0 0 1 3 2 4 front 1 7 5 2 4 rear enqueue(6) 6 6 http://ecomputernotes.com
Queue using Array front 2 5 7 1 rear 6 5 7 0 0 1 3 2 4 front 1 7 5 2 5 rear enqueue(8) 6 6 8 8 http://ecomputernotes.com
Queue using Array front 2 5 7 rear 6 5 7 1 0 1 3 2 4 front 7 5 2 5 rear dequeue() 6 6 8 8 http://ecomputernotes.com
Queue using Array front 2 5 rear 6 5 7 2 0 1 3 2 4 front 5 2 5 rear dequeue() 6 6 8 8 http://ecomputernotes.com
Queue using Array front 2 5 rear 6 5 7 2 0 1 3 2 4 front 5 2 7 rear enqueue(9) enqueue(12) 6 6 8 8 9 9 12 12 enqueue(21) ?? http://ecomputernotes.com
Queue using Array We have inserts and removal running in constant time but we created a new problem. Cannot insert new elements even though there are two places available at the start of the array. Solution: allow the queue to “wrap around”. http://ecomputernotes.com
Queue using Array Basic idea is to picture the array as a  circular array . front 2 5 rear 2 front 7 rear 6 8 9 12 6 5 7 0 1 3 2 4 5 2 6 8 9 12 http://ecomputernotes.com
Queue using Array void enqueue(int x) { rear = (rear+1)%size; array[rear] = x; noElements = noElements+1; } front 2 5 rear 2 front 0 rear 6 8 9 12 6 5 7 0 1 3 2 4 5 2 6 8 9 12 enqueue(21) 21 21 8 size 7 noElements http://ecomputernotes.com
Queue using Array int isFull() { return noElements == size; } int isEmpty() { return noElements == 0; } front 2 5 rear 2 front 1 rear 6 8 9 12 6 5 7 0 1 3 2 4 5 2 6 8 9 12 enqueue(7) 21 21 8 size 8 noElements 7 7 http://ecomputernotes.com
Queue using Array int dequeue() { int x = array[front]; front = (front+1)%size; noElements = noElements-1; return x; } front rear 4 front 1 rear 6 8 9 12 6 5 7 0 1 3 2 4 6 8 9 12 dequeue() 21 21 8 size 6 noElements 7 7 http://ecomputernotes.com
Use of Queues Out of the numerous uses of the queues, one of the most useful is  simulation . A simulation program attempts to model a real-world phenomenon. Many popular video games are simulations, e.g., SimCity, FlightSimulator Each object and action in the simulation has a counterpart in real world. http://ecomputernotes.com
Uses of Queues If the simulation is accurate, the result of the program should mirror the results of the real-world event. Thus it is possible to understand what occurs in the real-world without actually observing its occurrence. Let us look at an example. Suppose there is a bank with four tellers. http://ecomputernotes.com
Simulation of a Bank A customer enters the bank at a specific time (t 1 ) desiring to conduct a transaction. Any one of the four tellers can attend to the customer. The transaction (withdraw, deposit) will take a certain period of time (t 2 ). If a teller is free, the teller can process the customer’s transaction immediately and the customer leaves the bank at t 1 +t 2 . http://ecomputernotes.com

More Related Content

What's hot (20)

DOCX
Wap to implement bitwise operators
Harleen Sodhi
 
PDF
Exploring slides
akaptur
 
PDF
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
Sylvain Hallé
 
ODP
Exploiting Memory Overflows
Ankur Tyagi
 
PPT
Computer notes - Sorting
ecomputernotes
 
PPT
Stack queue
Harry Potter
 
PPT
03 stacks and_queues_using_arrays
tameemyousaf
 
PDF
Some Commonly asked Function/Objects Vs. header files (CBSE 12th Exam)
indiangarg
 
PPT
Algo>Queues
Ain-ul-Moiz Khawaja
 
PDF
matplotlib-installatin-interactive-contour-example-guide
Arulalan T
 
PPT
computer notes - Data Structures - 38
ecomputernotes
 
PDF
1 seaborn introduction
YuleiLi3
 
PPT
A Speculative Technique for Auto-Memoization Processor with Multithreading
Matsuo and Tsumura lab.
 
PDF
Csharp_Chap13
Mohamed Krar
 
PPT
Chapter 7: Queue data structure
Mahmoud Alfarra
 
RTF
Sorter
Thomas Knudstrup
 
PDF
Data structure lab manual
nikshaikh786
 
PDF
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
Sylvain Hallé
 
PPTX
Stack using Linked List
Sayantan Sur
 
PPTX
Data structures
Rokonuzzaman Rony
 
Wap to implement bitwise operators
Harleen Sodhi
 
Exploring slides
akaptur
 
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
Sylvain Hallé
 
Exploiting Memory Overflows
Ankur Tyagi
 
Computer notes - Sorting
ecomputernotes
 
Stack queue
Harry Potter
 
03 stacks and_queues_using_arrays
tameemyousaf
 
Some Commonly asked Function/Objects Vs. header files (CBSE 12th Exam)
indiangarg
 
Algo>Queues
Ain-ul-Moiz Khawaja
 
matplotlib-installatin-interactive-contour-example-guide
Arulalan T
 
computer notes - Data Structures - 38
ecomputernotes
 
1 seaborn introduction
YuleiLi3
 
A Speculative Technique for Auto-Memoization Processor with Multithreading
Matsuo and Tsumura lab.
 
Csharp_Chap13
Mohamed Krar
 
Chapter 7: Queue data structure
Mahmoud Alfarra
 
Data structure lab manual
nikshaikh786
 
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
Sylvain Hallé
 
Stack using Linked List
Sayantan Sur
 
Data structures
Rokonuzzaman Rony
 

Viewers also liked (20)

PPT
computer notes - Data Structures - 16
ecomputernotes
 
PPT
computer notes - Data Structures - 35
ecomputernotes
 
PPT
computer notes - Data Structures - 2
ecomputernotes
 
PPT
computer notes - Data Structures - 6
ecomputernotes
 
PPT
computer notes - Data Structures - 21
ecomputernotes
 
PPT
computer notes - Data Structures - 32
ecomputernotes
 
PPT
computer notes - Data Structures - 18
ecomputernotes
 
PPT
computer notes - Data Structures - 39
ecomputernotes
 
PPT
computer notes - Data Structures - 11
ecomputernotes
 
PPT
computer notes - Data Structures - 22
ecomputernotes
 
PPT
computer notes - Data Structures - 26
ecomputernotes
 
PPT
computer notes - Data Structures - 29
ecomputernotes
 
PPT
computer notes - Data Structures - 12
ecomputernotes
 
PPT
computer notes - Data Structures - 4
ecomputernotes
 
PPT
computer notes - Data Structures - 14
ecomputernotes
 
PPT
computer notes - Data Structures - 20
ecomputernotes
 
PPT
computer notes - Data Structures - 28
ecomputernotes
 
PPT
computer notes - Data Structures - 27
ecomputernotes
 
PPT
computer notes - Data Structures - 24
ecomputernotes
 
PPT
computer notes - Data Structures - 19
ecomputernotes
 
computer notes - Data Structures - 16
ecomputernotes
 
computer notes - Data Structures - 35
ecomputernotes
 
computer notes - Data Structures - 2
ecomputernotes
 
computer notes - Data Structures - 6
ecomputernotes
 
computer notes - Data Structures - 21
ecomputernotes
 
computer notes - Data Structures - 32
ecomputernotes
 
computer notes - Data Structures - 18
ecomputernotes
 
computer notes - Data Structures - 39
ecomputernotes
 
computer notes - Data Structures - 11
ecomputernotes
 
computer notes - Data Structures - 22
ecomputernotes
 
computer notes - Data Structures - 26
ecomputernotes
 
computer notes - Data Structures - 29
ecomputernotes
 
computer notes - Data Structures - 12
ecomputernotes
 
computer notes - Data Structures - 4
ecomputernotes
 
computer notes - Data Structures - 14
ecomputernotes
 
computer notes - Data Structures - 20
ecomputernotes
 
computer notes - Data Structures - 28
ecomputernotes
 
computer notes - Data Structures - 27
ecomputernotes
 
computer notes - Data Structures - 24
ecomputernotes
 
computer notes - Data Structures - 19
ecomputernotes
 
Ad

Similar to computer notes - Data Structures - 9 (20)

PDF
Polynomialmotilalanehrunationalinstitute.pdf
yugpadhiyar2006
 
PPT
Queues in C++ detailed explanation and examples .ppt
Jamiluddin39
 
PPT
Lecture 2d queues
Victor Palmar
 
PPTX
Queues_0748555555555555555555555526.pptx
nailapp2023
 
PPT
Queue in Data Structure
Muhazzab Chouhadry
 
PPTX
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
PPTX
Queue
Ayaz Akhtar
 
PPTX
Bsc cs ii dfs u-2 linklist,stack,queue
Rai University
 
PPTX
Queue data structures and operation on data structures
muskans14
 
PPTX
queues.pptx
Aadilhussain65
 
PPTX
The presention is about the queue data structure
gaurav77712
 
PPT
Queue data structure
Mekk Mhmd
 
PPTX
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
PDF
computer notes - Memory organization
ecomputernotes
 
PPTX
Queues
nidhisatija1
 
PPTX
Queue and its operations
V.V.Vanniaperumal College for Women
 
PPTX
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
PPTX
QUEUE in data-structure (classification, working procedure, Applications)
Mehedi Hasan
 
PPTX
Queue types of queue and algorithms and queue
geethikasudineni
 
PDF
Lab 07 (2).pdfbdvdyve dhdysbsnjsnsvdvydbdns
playstore9ha
 
Polynomialmotilalanehrunationalinstitute.pdf
yugpadhiyar2006
 
Queues in C++ detailed explanation and examples .ppt
Jamiluddin39
 
Lecture 2d queues
Victor Palmar
 
Queues_0748555555555555555555555526.pptx
nailapp2023
 
Queue in Data Structure
Muhazzab Chouhadry
 
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
Bsc cs ii dfs u-2 linklist,stack,queue
Rai University
 
Queue data structures and operation on data structures
muskans14
 
queues.pptx
Aadilhussain65
 
The presention is about the queue data structure
gaurav77712
 
Queue data structure
Mekk Mhmd
 
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
computer notes - Memory organization
ecomputernotes
 
Queues
nidhisatija1
 
Queue and its operations
V.V.Vanniaperumal College for Women
 
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
QUEUE in data-structure (classification, working procedure, Applications)
Mehedi Hasan
 
Queue types of queue and algorithms and queue
geethikasudineni
 
Lab 07 (2).pdfbdvdyve dhdysbsnjsnsvdvydbdns
playstore9ha
 
Ad

More from ecomputernotes (18)

PPT
computer notes - Data Structures - 30
ecomputernotes
 
PPT
computer notes - Data Structures - 15
ecomputernotes
 
DOC
Computer notes - Including Constraints
ecomputernotes
 
DOC
Computer notes - Date time Functions
ecomputernotes
 
DOC
Computer notes - Subqueries
ecomputernotes
 
DOC
Computer notes - Other Database Objects
ecomputernotes
 
PPT
computer notes - Data Structures - 31
ecomputernotes
 
PPT
computer notes - Data Structures - 13
ecomputernotes
 
DOC
Computer notes - Advanced Subqueries
ecomputernotes
 
DOC
Computer notes - Aggregating Data Using Group Functions
ecomputernotes
 
PPT
computer notes - Data Structures - 36
ecomputernotes
 
DOC
Computer notes - Enhancements to the GROUP BY Clause
ecomputernotes
 
DOC
Computer notes - Manipulating Data
ecomputernotes
 
DOC
Computer notes - Writing Basic SQL SELECT Statements
ecomputernotes
 
PPT
computer notes - Data Structures - 10
ecomputernotes
 
PPT
computer notes - Data Structures - 5
ecomputernotes
 
DOC
Computer notes - Controlling User Access
ecomputernotes
 
DOC
Computer notes - Using SET Operator
ecomputernotes
 
computer notes - Data Structures - 30
ecomputernotes
 
computer notes - Data Structures - 15
ecomputernotes
 
Computer notes - Including Constraints
ecomputernotes
 
Computer notes - Date time Functions
ecomputernotes
 
Computer notes - Subqueries
ecomputernotes
 
Computer notes - Other Database Objects
ecomputernotes
 
computer notes - Data Structures - 31
ecomputernotes
 
computer notes - Data Structures - 13
ecomputernotes
 
Computer notes - Advanced Subqueries
ecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
ecomputernotes
 
computer notes - Data Structures - 36
ecomputernotes
 
Computer notes - Enhancements to the GROUP BY Clause
ecomputernotes
 
Computer notes - Manipulating Data
ecomputernotes
 
Computer notes - Writing Basic SQL SELECT Statements
ecomputernotes
 
computer notes - Data Structures - 10
ecomputernotes
 
computer notes - Data Structures - 5
ecomputernotes
 
Computer notes - Controlling User Access
ecomputernotes
 
Computer notes - Using SET Operator
ecomputernotes
 

Recently uploaded (20)

PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PDF
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
PPTX
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
PPTX
Practical Applications of AI in Local Government
OnBoard
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PDF
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
PDF
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
PDF
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
Practical Applications of AI in Local Government
OnBoard
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
Kubernetes - Architecture & Components.pdf
geethak285
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 

computer notes - Data Structures - 9

  • 1. Class No.09 Data Structures http://ecomputernotes.com
  • 2. Memory Organization Code Static data Stack Heap Process 1 (browser) Process 3 (word) Process 4 (excel) Windows OS Process 2 (dev-c++) http://ecomputernotes.com
  • 3. Stack Layout during a call Here is stack layout when function F calls function G: Parameters(F) Local variables(F) Return address(F) Parameters(G) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) Parameters(G) Local variables(G) Return address(G) During execution of G After call At point of call sp sp sp http://ecomputernotes.com
  • 4. Queues A stack is LIFO (Last-In First Out) structure. In contrast, a queue is a FIFO (First-In First-Out ) structure. A queue is a linear structure for which items can be only inserted at one end and removed at another end. http://ecomputernotes.com
  • 5. Queue Operations Enqueue(X) – place X at the rear of the queue. Dequeue() -- remove the front element and return it. Front() -- return front element without removing it. IsEmpty() -- return TRUE if queue is empty, FALSE otherwise http://ecomputernotes.com
  • 6. Implementing Queue Using linked List: Recall Insert works in constant time for either end of a linked list. Remove works in constant time only. Seems best that head of the linked list be the front of the queue so that all removes will be from the front. Inserts will be at the end of the list. http://ecomputernotes.com
  • 7. Implementing Queue Using linked List: front 2 5 7 1 1 7 5 2 front rear rear http://ecomputernotes.com
  • 8. Implementing Queue Using linked List: front 2 5 7 1 1 7 5 2 front rear rear front 2 5 7 1 7 5 2 front rear rear dequeue() http://ecomputernotes.com
  • 9. Implementing Queue Using linked List: front 2 5 7 1 1 7 5 2 front rear rear front 2 5 7 9 7 5 2 front rear rear enqueue(9) 9 http://ecomputernotes.com
  • 10. Implementing Queue int dequeue() { int x = front->get(); Node* p = front; front = front->getNext(); delete p; return x; } void enqueue(int x) { Node* newNode = new Node(); newNode->set(x); newNode->setNext(NULL); rear->setNext(newNode); rear = newNode; } http://ecomputernotes.com
  • 11. Implementing Queue int front() { return front->get(); } int isEmpty() { return ( front == NULL ); } http://ecomputernotes.com
  • 12. Queue using Array If we use an array to hold queue elements, both insertions and removal at the front (start) of the array are expensive. This is because we may have to shift up to “n” elements. For the stack, we needed only one end; for queue we need both. To get around this, we will not shift upon removal of an element. http://ecomputernotes.com
  • 13. Queue using Array front 2 5 7 1 rear 6 5 7 0 0 1 3 2 4 front 1 7 5 2 3 rear http://ecomputernotes.com
  • 14. Queue using Array front 2 5 7 1 rear 6 5 7 0 0 1 3 2 4 front 1 7 5 2 4 rear enqueue(6) 6 6 http://ecomputernotes.com
  • 15. Queue using Array front 2 5 7 1 rear 6 5 7 0 0 1 3 2 4 front 1 7 5 2 5 rear enqueue(8) 6 6 8 8 http://ecomputernotes.com
  • 16. Queue using Array front 2 5 7 rear 6 5 7 1 0 1 3 2 4 front 7 5 2 5 rear dequeue() 6 6 8 8 http://ecomputernotes.com
  • 17. Queue using Array front 2 5 rear 6 5 7 2 0 1 3 2 4 front 5 2 5 rear dequeue() 6 6 8 8 http://ecomputernotes.com
  • 18. Queue using Array front 2 5 rear 6 5 7 2 0 1 3 2 4 front 5 2 7 rear enqueue(9) enqueue(12) 6 6 8 8 9 9 12 12 enqueue(21) ?? http://ecomputernotes.com
  • 19. Queue using Array We have inserts and removal running in constant time but we created a new problem. Cannot insert new elements even though there are two places available at the start of the array. Solution: allow the queue to “wrap around”. http://ecomputernotes.com
  • 20. Queue using Array Basic idea is to picture the array as a circular array . front 2 5 rear 2 front 7 rear 6 8 9 12 6 5 7 0 1 3 2 4 5 2 6 8 9 12 http://ecomputernotes.com
  • 21. Queue using Array void enqueue(int x) { rear = (rear+1)%size; array[rear] = x; noElements = noElements+1; } front 2 5 rear 2 front 0 rear 6 8 9 12 6 5 7 0 1 3 2 4 5 2 6 8 9 12 enqueue(21) 21 21 8 size 7 noElements http://ecomputernotes.com
  • 22. Queue using Array int isFull() { return noElements == size; } int isEmpty() { return noElements == 0; } front 2 5 rear 2 front 1 rear 6 8 9 12 6 5 7 0 1 3 2 4 5 2 6 8 9 12 enqueue(7) 21 21 8 size 8 noElements 7 7 http://ecomputernotes.com
  • 23. Queue using Array int dequeue() { int x = array[front]; front = (front+1)%size; noElements = noElements-1; return x; } front rear 4 front 1 rear 6 8 9 12 6 5 7 0 1 3 2 4 6 8 9 12 dequeue() 21 21 8 size 6 noElements 7 7 http://ecomputernotes.com
  • 24. Use of Queues Out of the numerous uses of the queues, one of the most useful is simulation . A simulation program attempts to model a real-world phenomenon. Many popular video games are simulations, e.g., SimCity, FlightSimulator Each object and action in the simulation has a counterpart in real world. http://ecomputernotes.com
  • 25. Uses of Queues If the simulation is accurate, the result of the program should mirror the results of the real-world event. Thus it is possible to understand what occurs in the real-world without actually observing its occurrence. Let us look at an example. Suppose there is a bank with four tellers. http://ecomputernotes.com
  • 26. Simulation of a Bank A customer enters the bank at a specific time (t 1 ) desiring to conduct a transaction. Any one of the four tellers can attend to the customer. The transaction (withdraw, deposit) will take a certain period of time (t 2 ). If a teller is free, the teller can process the customer’s transaction immediately and the customer leaves the bank at t 1 +t 2 . http://ecomputernotes.com

Editor's Notes

  • #3: End of lecture 8
  • #27: End of lecture 9